using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectPhapLy()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_PHAPLY
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectPhapLyPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_PHAPLY,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListPhapLy", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeletePhapLy(int id)
		{
			var deleteItem = _repPhapLy.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
            var fileDinhKem = deleteItem.FileDinhKem;

			var result = _repPhapLy.Delete(id);

			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
                if (fileDinhKem != string.Empty)
                {
                    var fullPath = Server.MapPath("/Content/Images/Items/" + fileDinhKem);
                    DeleteFile(fullPath);
                }
			}
			return Json(new{
				Success = result,
				RecordCount = _repPhapLy.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfPhapLy(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_PHAPLY,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdatePhapLy(FormCollection forms)
		{
			string id = forms.GetValues("PhapLy_ID")[0];
			string tieude = forms.GetValues("PhapLy_TieuDe")[0];
			string noidung = forms.GetValues("PhapLy_NoiDung")[0];
			string noidungrutgon = forms.GetValues("PhapLy_NoiDungRutGon")[0];
			var file = Request.Files["PhapLy_HinhMinhHoa"];
            var fileDinhKem = Request.Files["PhapLy_FileDinhKem"];

			string refName = "";
			var editItem = _repPhapLy.SelectByID(int.Parse(id));
			editItem.TieuDe = tieude;
			editItem.NoiDung = noidung;
			editItem.NoiDungRutGon = noidungrutgon;

			var result = false;
			if (file.ContentLength != 0)
			{
				result = UploadFile(file, "/Content/Images/Items/", id, ref refName);
				editItem.HinhMinhHoa = refName;
			}
            if (fileDinhKem.ContentLength != 0)
            {
                result = UploadFile(fileDinhKem, "/Content/Images/Items/", id, ref refName);
                editItem.FileDinhKem = refName;
            }
			result = _repPhapLy.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_PHAPLY,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfPhapLy", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertPhapLy(FormCollection forms)
		{
			string tieude = forms.GetValues("PhapLy_TieuDe")[0];
			string noidung = forms.GetValues("PhapLy_NoiDung")[0];
			string noidungrutgon = forms.GetValues("PhapLy_NoiDungRutGon")[0];
			var file = Request.Files["PhapLy_HinhMinhHoa"];
            var fileDinhKem = Request.Files["PhapLy_FileDinhKem"];

			string refName = "";
			var newItem = new PhapLy
			{
				TieuDe = tieude,
				NoiDung = noidung,
				NoiDungRutGon = noidungrutgon,
				HinhMinhHoa = "noimages.jpg",
                FileDinhKem = string.Empty
			};
			var result = _repPhapLy.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
					result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
					if (result)
					{
                        newItem.HinhMinhHoa = refName;
					}
				}
                if (fileDinhKem.ContentLength != 0)
                {
                    result = UploadFile(fileDinhKem, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
                    if (result)
                    {
                        newItem.FileDinhKem = refName;
                    }
                }
                _repPhapLy.Save();
			}
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_PHAPLY,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectPhapLy", CreateViewModel(data));
		}
	}
}